//
//  GeRenZiLiaoVC.m
//  jinwang
//
//  Created by jzb on 2018/3/9.
//  Copyright © 2018年 jzb. All rights reserved.
//

#import "GeRenZiLiaoVC.h"
#import "GeRenZiLiaoCell.h"
#import "JWHeaderImgVC.h"
#import "JWNicknameVC.h"
#import "JWSexVC.h"
#import "JWAddressVC.h"
#import "JWSignVC.h"
#import "JWMyQRCodeVC.h"

@interface GeRenZiLiaoVC ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate,TOCropViewControllerDelegate>
@property (nonatomic,assign) BOOL isAvatar;
@property (nonatomic,strong) GeRenZiLiaoCell *cell;

@end

@implementation GeRenZiLiaoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"个人资料";
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setUpTableView];

}
//配置tableView
-(void)setUpTableView{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    //注册cell
    
    [self registerCellWithCellClassArray:@[NSStringFromClass([GeRenZiLiaoCell class])]];
    
}
#pragma mark -UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    self.cell = [tableView dequeueReusableCellWithIdentifier:[GeRenZiLiaoCell getCellReuseIdentifier]];
    self.cell.contentView.backgroundColor = [UIColor clearColor];
    
    [self.cell.HeaderImgBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.NickNameBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.SexBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.AddressBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.GeXingQianMingBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.MyQRCodeBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.cell.HeaderImg sd_setImageWithURL:[NSURL URLWithString:k_user_headsmall] placeholderImage:[UIImage imageNamed:@"jw_img_404"]];
    self.cell.jinwangmaLabel.text = k_user_sn;
    return self.cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 440;
}
- (void)cellButtonOnclikc:(UIButton *)sender{
    
    if (sender.tag == 1000) {
        
//        JWHeaderImgVC *VC = [JWHeaderImgVC new];
//
//        [self.navigationController pushViewController:VC animated:YES];
        
        self.isAvatar = YES;
        [[CMAlertView sharedInstance] showActionSheetController:self title:@"更换头像" message:@"选择方式" cancelTitle:@"取消" destructive:nil actionBlock:^(NSInteger buttonTag) {
            switch (buttonTag) {
                case 0://取消
                {
                    
                }
                    break;
                case 1://相册
                {
                    UIImagePickerController *PickerImage = [[UIImagePickerController alloc]init];
                    PickerImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;//获取方式:通过相册
                    PickerImage.allowsEditing = NO;
                    PickerImage.delegate = self;
                    [self presentViewController:PickerImage animated:YES completion:nil];
                }
                    break;
                case 2://拍照
                {
                    UIImagePickerController *PickerImage = [[UIImagePickerController alloc]init];
                    PickerImage.sourceType = UIImagePickerControllerSourceTypeCamera;//获取方式:通过相机
                    PickerImage.allowsEditing = NO;
                    PickerImage.delegate = self;
                    [self presentViewController:PickerImage animated:YES completion:nil];
                }
                    break;
                    
                default:
                    break;
            }
        } otherButtonTitles:@"相册", @"拍照", nil];
        
    }else if (sender.tag == 1001){
        
        JWNicknameVC *VC = [JWNicknameVC new];
        
        [self.navigationController pushViewController:VC animated:YES];
        
    }else if (sender.tag == 1002){
        JWSexVC *VC = [JWSexVC new];
        
        [self.navigationController pushViewController:VC animated:YES];
        
    }else if (sender.tag == 1003){
        
        JWAddressVC *VC = [JWAddressVC new];
        
        [self.navigationController pushViewController:VC animated:YES];
    }else if (sender.tag == 1004){
        JWSignVC *VC = [JWSignVC new];
        
        [self.navigationController pushViewController:VC animated:YES];
        
    }else if (sender.tag == 1005){
        
        JWMyQRCodeVC *VC = [JWMyQRCodeVC new];
        
        [self.navigationController pushViewController:VC animated:YES];
    }
}

#pragma mark -相册 拍照
//PickerImage完成后的代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    //定义一个newPhoto，用来存放我们选择的图片。
    UIImage *newPhoto = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    //裁剪图片
    if (self.isAvatar) {
        TOCropViewController *cropVC = [[TOCropViewController alloc]initWithCroppingStyle:TOCropViewCroppingStyleCircular image:newPhoto];
        cropVC.delegate = self;
        [picker dismissViewControllerAnimated:YES completion:^{
            [self presentViewController:cropVC animated:YES completion:nil];
        }];
    }else{
        TOCropViewController *cropVC = [[TOCropViewController alloc]initWithCroppingStyle:TOCropViewCroppingStyleDefault image:newPhoto];
        cropVC.delegate = self;
        cropVC.customAspectRatio = CGSizeMake(kScreenWidth, kScreenWidth*(350.00/750.00));
        [picker dismissViewControllerAnimated:YES completion:^{
            [self presentViewController:cropVC animated:YES completion:nil];
        }];
    }
}
#pragma mark -TOCropViewControllerDelegate
- (void)cropViewController:(TOCropViewController *)cropViewController didCropToImage:(UIImage *)image withRect:(CGRect)cropRect angle:(NSInteger)angle
{
    [self updateImageViewWithImage:image fromCropViewController:cropViewController];
}

- (void)cropViewController:(TOCropViewController *)cropViewController didCropToCircularImage:(UIImage *)image withRect:(CGRect)cropRect angle:(NSInteger)angle
{
    [self updateImageViewWithImage:image fromCropViewController:cropViewController];
}

- (void)updateImageViewWithImage:(UIImage *)image fromCropViewController:(TOCropViewController *)cropViewController
{
    //上传
    //    [UserWebManager updateMyAvatar:image completed:^(UIImage *imageData) {
    //        if(!imageData){
    //            [self showHint:NSLocalizedString(@"setting.uploadFail", @"uploaded failed")];
    //            return;
    //        }
    //        [self showHint:NSLocalizedString(@"setting.uploadSuccess", @"uploaded successfully")];
    //    }];
    //    [self net_uploadImageWith:image];
    NSData *data = UIImageJPEGRepresentation(image, 0.5);
    //    [self net_QN_upload_withData:data];
    //    [cropViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
    
    [UploadImageTool uploadImage:image progress:^(NSString *key, float percent) {
        
    } success:^(NSString *url) {
        //        [hud removeFromSuperview];
        
//        NSLog(@"-=-=>>%@",url);
        
        //刷新
        [self.cell.HeaderImg sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"默认头像"]];
        [self net_makeUrl_setUserInfoWithHeaderImgURL:url];
        [cropViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
        
    } failure:^{
        
    }];
}
#pragma mark -网络请求

-(void)net_makeUrl_setUserInfoWithHeaderImgURL:(NSString *)url{
    [[DDNetworkManagerDate makeUrl_setUserInfo_with_token:k_user_token user_nickname:nil user_sex:nil user_sign:nil user_portrait:url] post_RequestFinshSuccess:^(id responseObject) {
        
        NSLog(@"-=-=>>%@",responseObject);
        if ([self requestFinshCode:responseObject isShowMessage:YES] == 1) {
            [kUserDefaults setObject:url forKey:@"user_headsmall"];
            //            [self goBack];
        }
        
    } failure:^(id errorObject) {
        [self showBottomMessag:@"网络请求失败"];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
